package com.xd.common.util;

import cn.hutool.core.util.RandomUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


public class OrderNumUtil {

    private static final String ORDER_CODE = "XDFW";
    private static final String REPAY_ORDER = "HKPAY";
    private static final String OFFLINE_REPAY_ORDER = "OLHKPAY";
    private static final String OUT_AMOUNT_ORDER = "FKPAY";
    private static final String TRANSFER_ORDER = "TRPAY";
    private static final String DEBT_ORDER = "DEPTPAY";
    private static final String CONTRACT_NO = "FM-HT";
    private static final String SEND_OUT_CODE = "OUTCODE";
    private static final String CERTIFICATION_CODE = "GZ-CODE";

    private static final String MORTGAGE__NO = "DY";


    private static final String RETURN_ORDER = "HT";

    private static final String REFUND_ORDER = "TK";



    private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};


    private  static final int maxLength = 4;


    private static String toCode(Integer userId) {
        String idStr = userId.toString();
        StringBuilder idsbs = new StringBuilder();
        for (int i = idStr.length() - 1; i >= 0; i--) {
            idsbs.append(r[idStr.charAt(i) - '0']);
        }
        return idsbs.append(getRandom(maxLength - idStr.length())).toString();
    }

    public static void main(String[] args) {
        String transferOrder = getTransferOrder();
        System.out.println(transferOrder);
    }


    public static String getDateTime() {
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }


    private static long getRandom(long n) {
        long min = 1, max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
        return rangeLong;
    }



    private static  synchronized String getCode(Integer userId) {
        userId = userId == null ? 10000 : userId;
        return getDateTime() + toCode(userId);
    }
    private static synchronized String getOrderCode() {
        return getDateTime();
    }

    private static  synchronized String getCodeByCompany(Integer companyId) {
        companyId = companyId == null ? 160000 : companyId;
        return getDateTime() + toCode(companyId);
    }

    public static String getOrderCodeByCompany(Integer companyId) {
        return ORDER_CODE + getCode(companyId);
    }


    public static String getOrderCode(Integer userId) {
        return ORDER_CODE + getCode(userId);
    }


    //生成合同编号
    public static String getContractNo() {
        return CONTRACT_NO + getDateTime()+ RandomUtil.randomLong(1000,9999);
    }

    //生成抵押订单编号
    public static String getMortgageNo() {
        return MORTGAGE__NO + getDateTime()+ RandomUtil.randomLong(100,999);
    }
    //放款短信时间戳
    public static String sendOutCode() {
        return SEND_OUT_CODE + System.currentTimeMillis()/1000 + RandomUtil.randomLong(100,999);
    }
    //债权商户订单号
    public static String getDeptOrder() {
        return DEBT_ORDER + getOrderCode()+ RandomUtil.randomLong(100,999);
    }
    //放款商户订单号
    public static String getOutAmountOrder() {
        return OUT_AMOUNT_ORDER + getOrderCode()+ RandomUtil.randomLong(100,999);
    }
    //代扣还款商户订单号
    public static String getRepayOrder() {
        return REPAY_ORDER + getOrderCode()+ RandomUtil.randomLong(100,999);
    }
    //公证订单号
    public static String getCertificationCode() {
        return CERTIFICATION_CODE + getOrderCode()+ RandomUtil.randomLong(100,999);
    }
    //线下还款商户订单号
    public static String getOfflineRepayOrder() {
        return OFFLINE_REPAY_ORDER + getOrderCode()+ RandomUtil.randomLong(100,999);
    }
    //电子转账商户订单号
    public static String getTransferOrder() {
        return TRANSFER_ORDER + getOrderCode()+ RandomUtil.randomLong(100,999);
    }


    public static String getReturnCode(Integer userId) {
        return RETURN_ORDER + getCode(userId);
    }



    public  static String getRefundCode(Integer userId) {
        return REFUND_ORDER + getCode(userId);
    }




}

